Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class ClassificacaoFiscal
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        If Not (Page.IsPostBack = True) Then

            btnPesquisar.Enabled = False

            'If (Session("blBack") = True) Then

            'If Not (Session("strOperacao") = "I") Then
            CarregaGVW()

            btnPesquisar.Enabled = True
            'End If

            'End If

        End If

        btnConsultar.Enabled = False
        btnIncluir.Enabled = True
        btnAlterar.Enabled = False
        btnExcluir.Enabled = False

    End Sub

    Private Sub CarregaGVW()

        Dim strWhere As String = ""

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT A.CodClassificacaoFiscal, A.DscClassificacaoFiscal, A.PercentualII, A.PercentualIPI, A.CodPIS, B.PercentualPIS, A.CodCOFINS, C.PercentualCOFINS, A.DtVigencia" & _
                     " FROM ClassificacaoFiscal A, pis B, cofins C" & _
                     " WHERE A.CodPIS = B.CodPIS AND A.CodCOFINS = C.CodCOFINS"

            If Not (Trim(txtCampoPesquisa.Text) = "") Then
                If (rbCodigo.Checked = True) Then
                    strWhere = " AND A.CodClassificacaoFiscal LIKE '%" & txtCampoPesquisa.Text & "%'"
                Else
                    strWhere = " AND A.DscClassificacaoFiscal LIKE '%" & txtCampoPesquisa.Text & "%'"
                End If
            End If

            If Not (strWhere = "") Then
                theQueryString = theQueryString & _
                         strWhere
            End If

            theQueryString = theQueryString & " ORDER BY A.CodClassificacaoFiscal;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub



    Protected Sub rbCodigo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbCodigo.CheckedChanged

        Try
            txtCampoPesquisa.Text = ""
            txtCampoPesquisa.MaxLength = 8
            txtCampoPesquisa.Width = 60

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub rbDescricao_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbDescricao.CheckedChanged

        Try
            txtCampoPesquisa.Text = ""
            txtCampoPesquisa.MaxLength = 60
            txtCampoPesquisa.Width = 380

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub btnPesquisar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPesquisar.Click
        CarregaGVW()
    End Sub

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()

            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnConsultar.Enabled = True
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        'End If

    End Sub

    Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodClassificacaoFiscal", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))

        Response.Redirect("ClassificacaoFiscalData.aspx")

    End Sub

    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodClassificacaoFiscal", "")

        Response.Redirect("ClassificacaoFiscalData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodClassificacaoFiscal", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))

        Response.Redirect("ClassificacaoFiscalData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Alterar

        Session.Add("strCodClassificacaoFiscal", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))

        Response.Redirect("ClassificacaoFiscalData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

End Class
